package com.ksc.storekeeper.client.view.components;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasCaption;
import com.google.gwt.user.client.ui.InlineHTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.ksc.storekeeper.client.view.validation.ValidatingListBox;
import com.ksc.storekeeper.client.view.validation.Validation;

public class FormItemListBox extends Composite implements HasCaption, HasClickHandlers {

	private static FormItemListBoxUiBinder uiBinder = GWT.create(FormItemListBoxUiBinder.class);

	interface FormItemListBoxUiBinder extends UiBinder<Widget, FormItemListBox> {
	}

	@UiField
	HTMLPanel panel;
	@UiField
	Label label;
	@UiField
	ValidatingListBox input;
	@UiField
	InlineHTML error;

	private boolean validation = false;
	private boolean required = false;

	public FormItemListBox() {
		initWidget(uiBinder.createAndBindUi(this));
		input.addItem("-Select-", "0");
	}

	@Override
	public String getCaption() {
		return label.getText();
	}

	/**
	 * Set caption/label for the item
	 */
	@Override
	public void setCaption(String text) {
		label.setText(text);
	}

	public void addItem(String display, String value) {
		input.addItem(display, value);
	}

	public void addItem(String value) {
		input.addItem(value, value);
	}

	/**
	 * Perform Validation for given attribute
	 * 
	 * @return
	 */
	public boolean validate() {
		boolean valid = true;
		if (validation) {
			input.setRequired(required);
			valid = input.validateListBox(input.getValue(input.getSelectedIndex())).equals(Validation.VALID) && valid;
			error.setText(input.getErrorMessage());
		}
		return valid;
	}

	/**
	 * Set parameter validate as true or false
	 * 
	 * @param value
	 */
	public void setValidation(boolean value) {
		validation = value;
	}

	/**
	 * Set parameter required as true or false
	 * 
	 * @param value
	 */
	public void setRequired(boolean value) {
		required = value;
	}

	/**
	 * Set style for form panel and input box
	 * 
	 * @param panelStyle
	 *            : Apply panel style
	 * @param inputStyle
	 *            : Apply input box style
	 */
	public void setStyle(String panelStyle, String inputStyle) {
		panel.setStyleName(panelStyle);
		input.setStyleName(inputStyle);
	}

	@Override
	public void setStyleName(String inputStyle) {
		input.setStyleName(inputStyle);
	}

	public int getSelectedIndex() {
		return input.getSelectedIndex();
	}

	public void setSelectedIndex(int index) {
		this.input.setSelectedIndex(index);
	}

	public String getValue() {
		return input.getValue(input.getSelectedIndex());
	}

	public void clearList() {
		input.clear();
		input.addItem("-Select-", "0");
		input.setSelectedIndex(0);
	}

	public String getErrorText() {
		return error.getText().trim();
	}

	public void clear() {
		error.setText("");
		input.removeInvalid();
		clearList();
	}

	public void removeInvalid() {
		error.setText("");
		input.removeInvalid();
	}

	public int getItemCount() {
		return input.getItemCount();
	}

	public String getItemText(int index) {
		return input.getItemText(index);
	}

	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		return input.addClickHandler(handler);
	}
}
